package com.chenyaowei.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.chenyaowei.common.utils.SecurityUtils;
import com.chenyaowei.member.service.ICywMemberStatisticsInfoService;
import com.chenyaowei.product.bo.CommentLevelTotalBO;
import com.chenyaowei.product.domain.CywProSpuComment;
import com.chenyaowei.product.domain.CywProSpuCommentReply;
import com.chenyaowei.product.mapper.CywProSpuCommentMapper;
import com.chenyaowei.product.service.ICywProSpuCommentReplyService;
import com.chenyaowei.product.service.ICywProSpuCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 商品评价Service业务层处理
 *
 * @author 陈耀威
 * @date 2023-12-03
 */
@Service
public class CywProSpuCommentServiceImpl implements ICywProSpuCommentService
{
    @Autowired
    private CywProSpuCommentMapper cywProSpuCommentMapper;

    @Autowired
    private ICywProSpuCommentReplyService commentReplyService;

    @Autowired
    private ICywMemberStatisticsInfoService memberStatisticsInfoService;

    /**
     * 查询商品评价
     *
     * @param id 商品评价主键
     * @return 商品评价
     */
    @Override
    public CywProSpuComment selectCywProSpuCommentById(Long id)
    {
        return cywProSpuCommentMapper.selectCywProSpuCommentById(id);
    }


    /**
     * 查询商品评价列表
     *
     * @param cywProSpuComment 商品评价
     * @return 商品评价
     */
    @Override
    public List<CywProSpuComment> selectCywProSpuCommentList(CywProSpuComment cywProSpuComment)
    {
        List<CywProSpuComment> list = cywProSpuCommentMapper.selectCywProSpuCommentList(cywProSpuComment);
        List<Long> commentIds = list.stream().map(CywProSpuComment::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(commentIds)){
            List<CywProSpuCommentReply> allReplyList = commentReplyService.selectListByCommentIds(commentIds);
            list.forEach(comment ->
                    comment.setReplyList(allReplyList.stream().filter(reply -> reply.getCommentId().equals(comment.getId())).collect(Collectors.toList()))
            );
        }
        return list;
    }

    /**
     * 新增商品评价
     *
     * @param cywProSpuComment 商品评价
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertCywProSpuComment(CywProSpuComment cywProSpuComment)
    {
        return cywProSpuCommentMapper.insertCywProSpuComment(cywProSpuComment);
    }

    /**
     * 修改商品评价
     *
     * @param cywProSpuComment 商品评价
     * @return 结果
     */
    @Override
    public int updateCywProSpuComment(CywProSpuComment cywProSpuComment)
    {
        return cywProSpuCommentMapper.updateCywProSpuComment(cywProSpuComment);
    }

    /**
     * 批量删除商品评价
     *
     * @param ids 需要删除的商品评价主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteCywProSpuCommentByIds(Long[] ids)
    {
        memberStatisticsInfoService.updateCommentCount(SecurityUtils.getUserId(), -ids.length);
        return cywProSpuCommentMapper.deleteCywProSpuCommentByIds(ids);
    }

    /**
     * 删除商品评价信息
     *
     * @param id 商品评价主键
     * @return 结果
     */
    @Override
    public int deleteCywProSpuCommentById(Long id)
    {
        return cywProSpuCommentMapper.deleteCywProSpuCommentById(id);
    }

    @Override
    public CommentLevelTotalBO countCommentLevelTotal(Long spuId) {
        return cywProSpuCommentMapper.countCommentLevelTotal(spuId);
    }
}
